/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.jfonia.view.main.header;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.Rectangle2D;
import javax.swing.JButton;
import org.jfonia.constants.ViewConstants;
import org.jfonia.images.ImageConstants;
import org.jfonia.images.ScaledImageIcon;
import org.jfonia.view.main.Project;
import org.jfonia.view.components.ITunesHeaderButtonUI;

/**
 *
 * @author Rik Bauwens
 */
public class NavigationButton extends JButton implements MouseListener
{
        private final int imageSize = 15;
        private final int imageRightMargin = 3;
        private final String imageSpacing = "  ";
        private final Image closeImage = new ScaledImageIcon(ImageConstants.LEADSHEET_CLOSE, "")
                                                   .setMaximumSide(imageSize).getImage();
        private boolean closeButtonVisible;

        public NavigationButton(String text)
        {
                super();
                setText(text);
                closeButtonVisible = false;
                setUI(new ITunesHeaderButtonUI());
                addMouseListener(this);
        }

        private void setCloseButtonVisible(boolean visible)
        {
                closeButtonVisible = visible;
                repaint();
        }

        @Override
        protected void paintComponent(Graphics g)
        {
                super.paintComponent(g);
                if(closeButtonVisible)
                        g.drawImage(closeImage, getWidth() - closeImage.getWidth(this)
                                - imageRightMargin, (getHeight() - closeImage.getHeight(this)) / 2, this);
        }

        public void mouseClicked(MouseEvent e)
        {
                Rectangle2D mouseRectangle = new Rectangle2D.Float(e.getX(), e.getY(),
                        (int) ViewConstants.MOUSE_POINTER_REGION_WIDTH,
                        (int) ViewConstants.MOUSE_POINTER_REGION_HEIGHT);
                Rectangle2D rectangle = new Rectangle2D.Float(getWidth() - closeImage.getWidth(this)
                                                             - imageRightMargin,
                                                             (getHeight() - closeImage.getHeight(this)) / 2,
                                                             closeImage.getWidth(this),
                                                             closeImage.getHeight(this));
                if(mouseRectangle.intersects(rectangle))
                        Project.getInstance().closeLeadSheet(Integer.parseInt(getActionCommand()));

        }

        @Override
        public void setText(String text)
        {
                super.setText(text + imageSpacing);
        }

        public void mousePressed(MouseEvent e) {}

        public void mouseReleased(MouseEvent e) {}

        public void mouseEntered(MouseEvent e)
        {
                setCloseButtonVisible(true);
        }

        public void mouseExited(MouseEvent e)
        {
                setCloseButtonVisible(false);
        }
}
